package com.leetroll.app.controller;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.leetroll.api.service.CategoryService;
import com.leetroll.api.service.ItemService;
import com.leetroll.api.service.MetricService;
import com.leetroll.api.service.SysConfigService;
import com.leetroll.app.service.CatMenuService;
import com.leetroll.common.entity.Metric;
import com.leetroll.common.entity.SysConfig;
import com.leetroll.common.enums.EConfigKey;
import com.leetroll.common.utils.JsonUtils;
import com.leetroll.common.vo.CatItemVO;
import com.leetroll.common.vo.CatMenuVO;
import com.leetroll.common.vo.ItemVO;
import com.leetroll.common.bean.ApiResponse;
import com.leetroll.common.entity.Category;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RefreshScope
@RequestMapping("/category")
@Api(value = "首页接口", tags = {"首页接口"})
@Slf4j
public class CategoryController {

    private final CategoryService categoryService;
    private final ItemService itemService;
    private final MetricService metricService;

    private final CatMenuService catMenuService;

    @Autowired
    public CategoryController(CategoryService categoryService, ItemService itemService, MetricService metricService, CatMenuService catMenuService) {
        this.categoryService = categoryService;
        this.itemService = itemService;
        this.metricService = metricService;
        this.catMenuService = catMenuService;
    }

    @ApiOperation(value = "获取所有分类", notes = "获取所有分类")
    @GetMapping("/all")
    public ApiResponse<List<Category>> queryAllCats() {
        List<Category> categories = categoryService.list().stream().sorted((Comparator.comparingInt(Category::getSequence))).collect(Collectors.toList());
        return ApiResponse.OK(categories);
    }

    @ApiOperation(value = "每个分类下获取4条item", notes = "每个分类下获取4条item")
    @GetMapping("/cats-with-4items")
    public ApiResponse<?> queryAllCatsWith4Items() {
        //查询所有分类
        List<Category> categories = categoryService.list().stream()
                .sorted((Comparator.comparingInt(Category::getSequence)))
                .collect(Collectors.toList());
        //每个分类下面查询4个最热门的item显示
        List<CatItemVO> catItemVOS = categories.stream().map(category -> {
            //查询4条热门item
            List<ItemVO> itemVOS = itemService.listHot4Items(category);
            return new CatItemVO(category.getId(), category.getName(), category.getTitle(), category.getSubTitle(), itemVOS);
        }).collect(Collectors.toList());
        return ApiResponse.OK(catItemVOS);
    }

    @ApiOperation(value = "每个分类下的metrics", notes = "每个分类下的metrics")
    @GetMapping("/metrics")
    public ApiResponse<List<Metric>> queryMetrics(@RequestParam(name = "catId", required = false) Integer catId) {
        if (catId == null) return ApiResponse.OK(new ArrayList<>());
        List<Metric> metrics = metricService.listByCat(catId);
        return ApiResponse.OK(metrics);
    }

    @ApiOperation(value = "获取分类菜单", notes = "获取分类菜单")
    @GetMapping("/cat-menu")
    public ApiResponse<List<CatMenuVO>> catMenu() {
        List<CatMenuVO> menuVOS = catMenuService.all();
        return ApiResponse.OK(menuVOS);
    }
}
